package project;
import java.sql.*;
import javax.swing.JTable;
import javax.swing.JOptionPane;
import module4.MySQLConnection;
import java.util.Vector;


public class StockItemDAO 
{
	private MySQLConnection msc = null;
	private PreparedStatement ps;
	private ResultSet rs = null;
	private Table table = null;
	private JTable jTable = null;
	
	public static String GET_STOCK_BY_TITLE_STATEMENT = 
		"SELECT stockTable.stockId, stockTable.title, stockTable.releaseDate," +
		"stockTable.status, stockTable.genre, stockTable.age, stockTable.description, " +
		"stockTable.runTime, stockTable.director, stockTable.actor1, stockTable.actor2, " +
		"pricecattable.catprice AS Price, stockTable.dateLastRented FROM stockTable, pricecattable " +
		"WHERE stockTable.catId = pricecattable.catId " +
		"AND title LIKE ?";
		
	public static String GET_STOCK_BY_BARCODE_STATEMENT = 
		"SELECT stockTable.stockId, stockTable.title, stockTable.releaseDate," +
		"stockTable.status, stockTable.genre, stockTable.age, stockTable.description, " +
		"stockTable.runTime, stockTable.director, stockTable.actor1, stockTable.actor2, " +
		"pricecattable.catprice AS Price, stockTable.dateLastRented FROM stockTable, pricecattable " +
		"WHERE stockTable.catId = pricecattable.catId " +
		"AND barcode LIKE ?";
		
	public static String GET_STOCK_BY_YEAR_STATEMENT = 
		"SELECT stockTable.stockId, stockTable.title, stockTable.releaseDate," +
		"stockTable.status, stockTable.genre, stockTable.age, stockTable.description, " +
		"stockTable.runTime, stockTable.director, stockTable.actor1, stockTable.actor2, " +
		"pricecattable.catprice AS Price, stockTable.dateLastRented FROM stockTable, pricecattable " +
		"WHERE stockTable.catId = pricecattable.catId " +
		"AND releaseDate LIKE ?";
		
	public static String GET_STOCK_BY_STATUS_STATEMENT = 
		"SELECT stockTable.stockId, stockTable.title, stockTable.releaseDate," +
		"stockTable.status, stockTable.genre, stockTable.age, stockTable.description, " +
		"stockTable.runTime, stockTable.director, stockTable.actor1, stockTable.actor2, " +
		"pricecattable.catprice AS Price, stockTable.dateLastRented FROM stockTable, pricecattable " +
		"WHERE stockTable.catId = pricecattable.catId " +
		"AND status LIKE ?";
		
	public static String GET_STOCK_BY_DLR_STATEMENT = 
		"SELECT stockTable.stockId, stockTable.title, stockTable.releaseDate," +
		"stockTable.status, stockTable.genre, stockTable.age, stockTable.description, " +
		"stockTable.runTime, stockTable.director, stockTable.actor1, stockTable.actor2, " +
		"pricecattable.catprice AS Price, stockTable.dateLastRented FROM stockTable, pricecattable " +
		"WHERE stockTable.catId = pricecattable.catId " +
		"AND dateLastRented LIKE ?";
		
	public static String GET_RENTAL_FROM_BOOKING_STATEMENT =
		"SELECT stockTable.stockId, stockTable.title, " +
		"pricecattable.catprice AS Price " +
		"FROM stockTable, pricecattable " +
		"WHERE stockTable.catId = pricecattable.catId " +
		"AND stockId = ?";
		
	public static String GET_CUSTOMER_BY_CARD_STATEMENT = 
		"SELECT custId, name, surname, custStatus, creditAmount, phone1" +
		" FROM customerTable WHERE custId LIKE ?";
		
	public static String UPDATE_STOCK_RENTAL_STATEMENT =
		"UPDATE stockTable SET status = ?, dateLastRented = current_date() " +
		"WHERE stockId = ?";
		
	public static String UPDATE_STOCK_RETURN_STATEMENT = 
		"UPDATE stockTable SET status = ? WHERE stockId = ?";
		
	public static String UPDATE_STOCK_STATUS_STATEMENT = 
		"UPDATE stockTable SET status = ? " +
		"WHERE stockId = ? ";
		
	public static String ADD_RENT_EVENT_STATEMENT =
		"INSERT INTO eventTable VALUES(?, current_date(), 'Loan', ?, ?, ?, current_time())";
		
	public static String ADD_RETURN_EVENT_STATEMENT  =
		"INSERT INTO eventTable VALUES(?, current_date(), 'Return', ?, ?, ?, current_time())";
	
	public static String ADD_REFUND_EVENT_STATEMENT = 
		"INSERT INTO eventTable VALUES(?, current_date(), 'Credit', ?, ?, ?, current_time())";
		
	public static String ADD_BOOKING_EVENT_STATEMENT =
		"INSERT INTO eventTable VALUES(?, current_date(), 'Booking', ?, ?, ?, current_time())";
		
	public static String ADD_CUSTBOOKING_STATEMENT = 
		"INSERT INTO custBookingTable VALUES(?, ?, ?)";
		
	public static String ADD_CUSTEVENT_STATEMENT = 
		"INSERT INTO custEventTable VALUES(?, ?, ?)";
		
	public static String CHECK_BOOKING_DATE_STATEMENT =
		"SELECT stockId, dateBooked FROM custBookingTable WHERE stockId = ? " +
		"AND (dateBooked = ? OR dateBooked = ? OR dateBooked = ?)";
		
	public static String GET_CUST_BOOKINGS = 
		"SELECT custbookingtable.custid, custbookingtable.stockid, stocktable.title, custbookingtable.datebooked " +
		"FROM custbookingtable, stocktable " +
		"WHERE custbookingtable.custid = ? AND custbookingtable.stockid = stocktable.stockid " +
		"AND datebooked >= current_date()";
		
	public static String GET_ALL_BOOKINGS = 
		"SELECT custbookingtable.custid, custbookingtable.stockid, stocktable.title, custbookingtable.datebooked " +
		"FROM custbookingtable, stocktable " +
		"WHERE custbookingtable.stockid = stocktable.stockid " +
		"AND datebooked >= current_date()";
		
	public static String DELETE_CUST_BOOKING = 
		"DELETE FROM custbookingtable " +
		"WHERE custid = ? AND stockid = ? AND datebooked = ?";
	
	public static String ADJUST_CREDIT_STATEMENT = 
		"UPDATE customerTable SET creditamount = (creditAmount + ?) " +
		"WHERE custId = ?";
		
	public static String REFUND_STATEMENT = 
		"UPDATE customertable SET creditamount = (creditamount + (" +
		"SELECT pricecattable.catPrice " +
		"FROM pricecattable, stocktable " +
		"WHERE pricecattable.catid = stocktable.catid " +
		"AND stocktable.stockid = ?)) " +
		"WHERE custid = (" +
		"SELECT eventtable.custid " +
		"FROM eventtable, custeventtable, stocktable " +
		"WHERE eventtable.eventid = custeventtable.eventid " +
		"AND custeventtable.stockid = stocktable.stockid " +
		"AND stocktable.stockid = ? " +
		"AND eventtable.eventtype = 'Loan' " +
		"GROUP BY datelastrented)";
	
	public static String GET_STOCK_PRICE_STATEMENT = 
		"SELECT pricecattable.catprice AS 'Price' " +
		"FROM pricecattable, stocktable " +
		"WHERE pricecattable.catid = stocktable.catid " +
		"AND stocktable.stockid = ?";
		
	public static String GET_CUSTID_FROM_STOCKID_STATEMENT = 
		"SELECT eventtable.custid AS 'custId' " +
		"FROM eventtable, custeventtable, stocktable " +
		"WHERE eventtable.eventid = custeventtable.eventid " +
		"AND custeventtable.stockid = stocktable.stockid " +
		"AND stocktable.stockid = ? " +
		"AND eventtable.eventtype = 'Loan' " +
		"GROUP BY datelastrented";
		
		
		
	//public static String
		
	public void closeConnection()
	{
		try
		{
			rs.close();
			ps.close();
			msc.getConnection().close();
			System.out.println("Connection to " + msc.getDBName() + " closed.");
		}
		catch(SQLException sqle)
		{
			System.out.println(sqle.getMessage());
		}
		
	}
	
	
	public JTable getStockByTitle(String title)
	{
		try
		{
			connect();
			ps = msc.getConnection().prepareStatement(
				GET_STOCK_BY_TITLE_STATEMENT,
				ResultSet.TYPE_SCROLL_SENSITIVE,
				ResultSet.CONCUR_READ_ONLY);
			ps.setString(1, title + "%");
			rs = ps.executeQuery();
			table = new Table();
			jTable = table.createStockSearchTable(rs);
			closeConnection();
		}
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}
		catch(SQLException sqle)
		{
			System.out.println(sqle.getMessage());
		}
		return jTable;
	
	}
	
	public JTable getStockByBarcode(String barcode)
	{
		try
		{
			connect();
			ps = msc.getConnection().prepareStatement(
				GET_STOCK_BY_BARCODE_STATEMENT,
				ResultSet.TYPE_SCROLL_SENSITIVE,
				ResultSet.CONCUR_READ_ONLY);
			ps.setString(1, barcode + "%");
			rs = ps.executeQuery();
			table = new Table();
			jTable = table.createStockSearchTable(rs);
			closeConnection();
		   	
		}
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}
		catch(SQLException sqle)
		{
			System.out.println(sqle.getMessage());
		}
		return jTable;
	
	}
	
	
	public JTable getStockByYear(String year)
	{
		try
		{
			connect();
			ps = msc.getConnection().prepareStatement(
				GET_STOCK_BY_YEAR_STATEMENT,
				ResultSet.TYPE_SCROLL_SENSITIVE,
				ResultSet.CONCUR_READ_ONLY);
			ps.setString(1, year + "%");
			rs = ps.executeQuery();
			table = new Table();
			jTable = table.createStockSearchTable(rs);
			closeConnection();
			
		}
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}
		catch(SQLException sqle)
		{
			System.out.println(sqle.getMessage());
		}
		return jTable;
	}
	public JTable getStockByStatus(String status)
	{
		try
		{
			connect();
			ps = msc.getConnection().prepareStatement(
				GET_STOCK_BY_STATUS_STATEMENT,
				ResultSet.TYPE_SCROLL_SENSITIVE,
				ResultSet.CONCUR_READ_ONLY);
			ps.setString(1, status + "%");
			rs = ps.executeQuery();
			table = new Table();
			jTable = table.createStockSearchTable(rs);
			closeConnection();
			
		}
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}
		catch(SQLException sqle)
		{
			System.out.println(sqle.getMessage());
		}
		return jTable;
	}
	public JTable getStockByDLS(String DLR)//DLR = Date Last Rented
	{
		try
		{
			connect();
			ps = msc.getConnection().prepareStatement(
				GET_STOCK_BY_DLR_STATEMENT,
				ResultSet.TYPE_SCROLL_SENSITIVE,
				ResultSet.CONCUR_READ_ONLY);
			ps.setString(1, DLR + "%");
			rs = ps.executeQuery();
			table = new Table();
			jTable = table.createStockSearchTable(rs);
			closeConnection();
			
		}
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}
		catch(SQLException sqle)
		{
			System.out.println(sqle.getMessage());
		}
		return jTable;
	}
	
	public JTable getCustomerByCard(String cardNo)
	{
		try
		{
			connect();
			ps = msc.getConnection().prepareStatement(
				GET_CUSTOMER_BY_CARD_STATEMENT,
				ResultSet.TYPE_SCROLL_SENSITIVE,
				ResultSet.CONCUR_READ_ONLY);
			ps.setString(1, cardNo + "%");
			rs = ps.executeQuery();
			table = new Table();
			jTable = table.createStockSearchTable(rs);
			closeConnection();
			
		}
		catch(ClassNotFoundException cnfe)
		{
			System.out.println(cnfe.getMessage());
		}
		catch(SQLException sqle)
		{
			System.out.println(sqle.getMessage());
		}
		return jTable;
	}
	//GET LIST OF A CUSTOMER'S BOOKINGS
	//--------------------------------
	public JTable getCustBookings(String custId)
	{
		try
		{
			connect();
			ps = msc.getConnection().prepareStatement(
				GET_CUST_BOOKINGS,
				ResultSet.TYPE_SCROLL_SENSITIVE,
				ResultSet.CONCUR_READ_ONLY);
			ps.setString(1, custId);
			rs = ps.executeQuery();
			table = new Table();
			jTable = table.createStockSearchTable(rs);
			closeConnection();
			
		}
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}
		catch(SQLException sqle)
		{
			System.out.println(sqle.getMessage());
		}
		return jTable;
	}
	//GET LIST OF ALL BOOKINGS
	//------------------------
	public JTable getAllBookings()
	{
		try
		{
			connect();
			Statement stmt = msc.getConnection().createStatement(
				ResultSet.TYPE_SCROLL_SENSITIVE,
				ResultSet.CONCUR_READ_ONLY);
			rs = stmt.executeQuery(GET_ALL_BOOKINGS);
			table = new Table();
			jTable = table.createStockSearchTable(rs);
			rs.close();
			stmt.close();
			msc.getConnection().close();
			System.out.println("Connection to " + msc.getDBName() + " closed.");
		}
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}
		catch(SQLException sqle)
		{
			System.out.println(sqle.getMessage());
		}
		return jTable;
	}
	
	//DELETE A CUSTOMER BOOKING
	//-------------------------
	public void deleteBooking(String custId, String stockId, String dateBooked)
	{
		try
		{
			connect();
			ps = msc.getConnection().prepareStatement(
				DELETE_CUST_BOOKING,
				ResultSet.TYPE_SCROLL_SENSITIVE,
				ResultSet.CONCUR_READ_ONLY);
			ps.setString(1, custId);
			ps.setString(2, stockId);
			ps.setString(3, dateBooked);
			System.out.println("Rows affected = " + ps.executeUpdate());
			closeConnection();
			
		}
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}
		catch(SQLException sqle)
		{
			System.out.println(sqle.getMessage());
		}
	}
	
	//ADD RENTAL FROM BOOKING
	//-----------------------
	public Object [] getCheckOutVect(String stockId)
	{
		Object [] objectArray = null;
		try
		{
			connect();
			ps = msc.getConnection().prepareStatement(
					GET_RENTAL_FROM_BOOKING_STATEMENT,
					ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			ps.setString(1, stockId);
			rs = ps.executeQuery();
			rs.first();
			objectArray = new Object[]{
				rs.getString(1),
				rs.getString(2),
				rs.getDouble(3)};
			closeConnection();
		}
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}
		catch(SQLException sqle)
		{
			System.out.println(sqle.getMessage());
		}
		return objectArray;
		
	}
	
	//CONFIRM RENTAL
	//--------------
	
	public void confirmRental(double value, String custId, String empId, Vector stockId)
	{
		try
		{
			//ADD EVENT
			//---------
			String eventId = generateEventId();
			connect();
			ps = msc.getConnection().prepareStatement(
				ADD_RENT_EVENT_STATEMENT);
			ps.setString(1, eventId);
			ps.setDouble(2, (value));
			ps.setString(3, custId);
			ps.setString(4, empId);
			System.out.println("Event Rows affected: " + ps.executeUpdate());
			System.out.println("Event added.\n");
			
			//UPDATE STOCK STATUS
			//-------------------
			ps = msc.getConnection().prepareStatement(
				UPDATE_STOCK_RENTAL_STATEMENT);
			for(int i = 0; i < stockId.size(); i++)
			{
				String temp = (String)stockId.elementAt(i);
				ps.setString(1, "OUT");
				ps.setString(2, temp);
				System.out.println("Stock Rows affected: " + ps.executeUpdate());
				System.out.println("Stock table updated " + i + ".\n");
			}
			
			
			//ADD TO CUSTEVENTTABLE
			//---------------------
			ps = msc.getConnection().prepareStatement(
				ADD_CUSTEVENT_STATEMENT);
			for(int i = 0; i < stockId.size(); i++)
			{
				String temp1 = (String)stockId.elementAt(i);
				ps.setString(1, eventId);
				ps.setString(2, temp1);
				ps.setInt(3, i + 1);
				System.out.println("CustEvent Rows affected: " + ps.executeUpdate());
				System.out.println("Customer Event added " + i + ".\n");
			}
			
			closeConnection();
		}
		catch(ClassNotFoundException cnfe)
		{
			System.out.println(cnfe.getMessage());
		}
		catch(SQLException sqle)
		{
			System.out.println(sqle.getMessage());
		}
	}
	
	//CONFIRM RETURN
	//--------------
	public void confirmReturn(double value, String custId, String empId,
							Vector stockId, String status)
	{
		try
		{
			//ADD EVENT
			//---------
			String eventId = generateEventId();
			connect();
			ps = msc.getConnection().prepareStatement(
				ADD_RETURN_EVENT_STATEMENT);
			ps.setString(1, eventId);
			ps.setDouble(2, value);
			ps.setString(3, custId);
			ps.setString(4, empId);
			System.out.println("Event Table rows affected: " + ps.executeUpdate());

			//UPDATE STOCK
			//------------
			ps = msc.getConnection().prepareStatement(
				UPDATE_STOCK_RETURN_STATEMENT);
			for(int i = 0; i < stockId.size(); i++)
			{
				String temp = (String)stockId.elementAt(i);
				ps.setString(1, status);
				ps.setString(2, temp);
				System.out.println("Stock Table rows updated: " + ps.executeUpdate());
			}
			
			//ADD CUSTEVENT
			//-------------
			ps = msc.getConnection().prepareStatement(
				ADD_CUSTEVENT_STATEMENT);
			for(int i = 0; i < stockId.size(); i++)
			{
				String temp1 = (String)stockId.elementAt(i);
				ps.setString(1, eventId);
				ps.setString(2, temp1);
				ps.setInt(3, i + 1);
				System.out.println("CustEvent Rows affected: " + ps.executeUpdate());
				System.out.println("Customer Event added " + i + ".\n");
			}
			
			
			closeConnection();
		}
		catch(ClassNotFoundException cnfe)
		{
			System.out.println("Exception caught in confirmReturn in StockItemDAO");
			cnfe.printStackTrace();
		}
		catch(SQLException sqle)
		{
			System.out.println("Exception caught in confirmReturn in StockItemDAO");
			sqle.printStackTrace();
		}
	}
	
	//REFUND
	//------
	public void refund(String stockId, String status, String empId)
	{
		try
		{
			//ADD AMOUNT TO CLIENT CREDIT
			//---------------------------
			connect();
			ps = msc.getConnection().prepareStatement(
				REFUND_STATEMENT);
			ps.setString(1, stockId);
			ps.setString(2, stockId);
			System.out.println("CustomerTable rows updated: " + ps.executeUpdate());
			
			//ADD REFUND EVENT
			//----------------
			ps = msc.getConnection().prepareStatement(
				GET_STOCK_PRICE_STATEMENT);
			ps.setString(1, stockId);
			rs = ps.executeQuery();
			rs.first();
			double tempPrice = rs.getDouble("Price");
			System.out.println("Price = " + tempPrice);
			
			ps = msc.getConnection().prepareStatement(
				GET_CUSTID_FROM_STOCKID_STATEMENT);
			ps.setString(1, stockId);
			rs = ps.executeQuery();
			rs.first();
			String custId = rs.getString("custId");
			System.out.println("CustId = " + custId);
			rs.close();
			
			String eventId = generateEventId();
			
			ps = msc.getConnection().prepareStatement(
				ADD_REFUND_EVENT_STATEMENT);
			ps.setString(1, eventId);
			ps.setDouble(2, tempPrice * -1);
			ps.setString(3, custId);
			ps.setString(4, empId);
			System.out.println("Event Rows affected: " + ps.executeUpdate());
			System.out.println("Event added.\n");
			
			ps = msc.getConnection().prepareStatement(
				UPDATE_STOCK_STATUS_STATEMENT);
			ps.setString(1, status);
			ps.setString(2, stockId);
			System.out.println("Stock Table Rows affected: " + ps.executeUpdate());
			closeConnection();	
		}
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}
		catch(SQLException sqle)
		{
			sqle.printStackTrace();
		}
		
		
	}
	
	
	//MAKE BOOKING
	//------------
	public void makeBooking(double value, String custId, String empId,
		Vector stockId, Vector date)
	{
		try
		{
			//ADD EVENT
			//---------
			String eventId = generateEventId();
			connect();
			ps = msc.getConnection().prepareStatement(
				ADD_BOOKING_EVENT_STATEMENT);
			ps.setString(1, eventId);
			ps.setDouble(2, 0.0);
			ps.setString(3, custId);
			ps.setString(4, empId);
			System.out.println("Event Rows affected: " + ps.executeUpdate());
			System.out.println("Event added.\n");
			
			//ADD CUSTBOOKING
			//---------------
			ps = msc.getConnection().prepareStatement(
				ADD_CUSTBOOKING_STATEMENT);
			for(int i = 0; i < stockId.size(); i++)
			{
				String stockTemp = (String)stockId.elementAt(i);
				String dateTemp = (String)date.elementAt(i);
				ps.setString(1, stockTemp);
				ps.setString(2, custId);
				ps.setString(3, dateTemp);
				System.out.println("Event Rows affected: " + ps.executeUpdate());
				System.out.println("Booking added.\n");
			}
			
			//ADD CUSTEVENT
			//-------------
			ps = msc.getConnection().prepareStatement(
					ADD_CUSTEVENT_STATEMENT);
			for(int i = 0; i < stockId.size(); i++)
			{
				String temp1 = (String)stockId.elementAt(i);
				ps.setString(1, eventId);
				ps.setString(2, temp1);
				ps.setInt(3, i + 1);
				System.out.println("CustEvent Rows affected: " + ps.executeUpdate());
				System.out.println("Customer Event added " + i + ".\n");
			}
			
			closeConnection();
		}
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}
		catch(SQLException sqle)
		{
			sqle.printStackTrace();
		}
		
	}
	
	//CHECK BOOKING CLASHES
	//---------------------
	public boolean checkBookingDate(String stockId, String date)
	{	
		boolean flag = false;
		try
		{
			
			
			connect();
			ps = msc.getConnection().prepareStatement(
				CHECK_BOOKING_DATE_STATEMENT);
			ps.setString(1, stockId);
			ps.setString(2, date);
			Statement stmt = msc.getConnection().createStatement(
				ResultSet.TYPE_SCROLL_SENSITIVE,
				ResultSet.CONCUR_READ_ONLY);
			ResultSet checkRS = stmt.executeQuery("SELECT date_sub('" + date + "', INTERVAL 1 DAY) AS 'date'");
			System.out.println("date_add: " + checkRS);
			checkRS.next();
			ps.setString(3, checkRS.getString("date"));
			checkRS = stmt.executeQuery("SELECT date_add('" + date + "', INTERVAL 1 DAY) AS 'date'");
			System.out.println("date_add: " + checkRS);
			checkRS.next();
			ps.setString(4, checkRS.getString("date"));
			ResultSet bookingRS = ps.executeQuery();
			if(bookingRS.next())
			{
				flag = true;
			}
		}
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}
		catch(SQLException sqle)
		{
			sqle.printStackTrace();
		}
		return flag;
	}
	
	//GET AMOUNT OF BOOKINGS
	//----------------------
	public int getBookings(String custId)
	{
		int temp = -1;
		try
		{
			connect();
			Statement stmt = msc.getConnection().createStatement(
				ResultSet.TYPE_SCROLL_SENSITIVE,
				ResultSet.CONCUR_READ_ONLY);
			rs = stmt.executeQuery(
				"select count(*) from custbookingtable where custId = '" + 
				custId + "' and datebooked >= current_date()");
			rs.next();
			temp = rs.getInt(1);
			System.out.print("temp = " + temp);
			rs.close();
			msc.getConnection().close();
			System.out.println("Connection to " + msc.getDBName() + " closed.");
		}
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}
		catch(SQLException sqle)
		{
			sqle.printStackTrace();
		}
		return temp;
	}
	
	//GENERATE EVENT ID
	//-----------------
	public String generateEventId() throws ClassNotFoundException, SQLException
	{
		String tempId = "EV" + (int)((Math.random()*90000000)+10000000);
		connect();
		Statement stmt = msc.getConnection().createStatement(
			ResultSet.TYPE_SCROLL_SENSITIVE,
			ResultSet.CONCUR_READ_ONLY);
		//rs = stmt.executeQuery("SELECT eventId FROM eventTable");
		//rs.first();
		
		while(true)
		{
			rs = stmt.executeQuery("SELECT eventId FROM eventTable");
			tempId = "EV" + (int)((Math.random()*90000000)+10000000);
			rs.first();
			while(true)
			{
				if(tempId == rs.getString("eventId") || rs.isLast())
				{
					break;
				}
				else
				{
					rs.next();
				}
			}
			if(tempId == rs.getString("eventId"))
			{
				continue;
			}
			else
			{
				rs.close();
				stmt.close();
				//msc.getConnection().close();
				//System.out.println("Connection to " + msc.getDBName() + " closed.");
				break;
			}
			
		}
		return tempId;
	}
	private void connect() throws ClassNotFoundException, SQLException
	{
		msc = new MySQLConnection("127.0.0.1", "TurtleData", "", "");
		msc.registerDriver();
		msc.connect();
	}
	public Connection getConnection()
	{
		try
		{
			connect();
		}
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}
		catch(SQLException sqle)
		{
			sqle.printStackTrace();
		}
		return msc.getConnection();
	}
	//--------------------------------------
}
